<template>
	<el-dialog :visible.sync="dialog" :close-on-click-modal="false" :title="title" append-to-body width="600px"
	           @close="cancel">
	    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
	        <el-form-item label="是否公开" prop="isPublic">
				 <el-switch v-model="isPublic"></el-switch>
	        </el-form-item>
	        <el-form-item label="笔记内容" prop="noteContent">
	           <el-input type="textarea" v-model="form.noteContent" rows="9" :autosize="{ minRows: 9, maxRows: 12}"
	                     placeholder="笔记内容,400字以内" style="width: 390px;"></el-input>
	        </el-form-item>
	    </el-form>
	    <div slot="footer" class="dialog-footer">
	        <el-button type="text" @click="cancel">取消</el-button>
	        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
	    </div>
	</el-dialog>
</template>

<script>
	import { addTrainNote } from '@/api/train'
    export default {
        data() {
            return {
                loading: false,
                dialog: false,
                title: '课程笔记',
				isPublic:false,
                form: {isPublic:null, noteContent: "",courseId:"",chapterId:""},
                rules: {
                    noteContent: [
                        {required: true, message: '请输入评价内容,400字以内',trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            doSubmit() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
						if(this.form.noteContent.length>400){
							this.$message.error(`字数不能超多400字！`)
							return
						}
                        this.loading = true
						if(this.isPublic){
							this.form.isPublic=1
						}else{
							this.form.isPublic=0
						}
                        addTrainNote(this.form).then(res => {
                            this.cancel()
							//this.$parent.init()
							this.$parent.$refs.notesRef.init();
                            this.$message.success(`笔记保存成功！`)
                        }).catch(err => {
                            this.loading = false
                        })
                    } else {
                        return false
                    }
                })
            },
            cancel() {
                this.dialog = false
                this.loading = false
                this.$refs['form'].resetFields()
                this.form = {id: '', noteContent: ''}
            }
        }
    }
</script>

<style>
</style>